package com.hualan.service.impl;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.JsonResult;
import com.hualan.bean.PageResult;
import com.hualan.bean.PanghuCommunity;
import com.hualan.dao.CommunityListDao;
import com.hualan.dao.CommunityUserDao;
import com.hualan.dao.ShopCommunityDao;
import com.hualan.dao.impl.CommunityListDaoImpl;
import com.hualan.dao.impl.CommunityUserDaoImpl;
import com.hualan.dao.impl.ShopCommunityDaoImpl;
import com.hualan.service.CommunityListService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 小区管理业务实现
 */
public class CommunityListServiceImpl implements CommunityListService {
    private CommunityListDao communityListDao;
    private CommunityUserDao communityUserDao;
    private ShopCommunityDao shopCommunityDao;

    //无参构造 业务类实例化的同时实例化其它类
    public CommunityListServiceImpl() {
        communityListDao = new CommunityListDaoImpl();
        communityUserDao = new CommunityUserDaoImpl();
        shopCommunityDao = new ShopCommunityDaoImpl();
    }

    //获取小区信息 json字符串
    @Override
    public String communityList(String name, String startTime, String endTime, String pageNum, String pageSize, String communityName) throws ParseException {
        //处理时间,格式化时间
        if (startTime != null && !startTime.equals("") && endTime != null && !endTime.equals("")){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            startTime = simpleDateFormat.format(simpleDateFormat.parse(startTime));
            endTime = simpleDateFormat.format(simpleDateFormat.parse(endTime));
        }

        //处理分页
        //先判断传入的页码，每页数量 是否为空，为空赋默认值
        if (pageNum == null || pageNum.equals("")){
            pageNum = "1";
        }
        if (pageSize == null || pageSize.equals("")){
            pageSize = "10";
        }
        //起始值 （页码-1）*每页数量
        int pNum = (Integer.valueOf(pageNum) - 1) * Integer.valueOf(pageSize);
        List<PanghuCommunity> communityList = communityListDao.communityList(name, startTime, endTime, pNum+"", pageSize, communityName);
        for (PanghuCommunity community : communityList){
            //根据小区id获取用户数量
            Long communityUserCount = communityUserDao.getCommunityUserCount(community.getCommunityId());
            //为每个小区设置用户数量
            community.setCommunityUserCount(communityUserCount.intValue());

            //根据小区id获取关联门店数量
            Long shopCommunityCount = shopCommunityDao.getShopCommunityCount(community.getCommunityId());
            //为每个小区设置关联门店数量
            community.setCommunityShopCount(shopCommunityCount.intValue());
        }
        Long total = communityListDao.CommunityTotal();

        PageResult pageResult = new PageResult<>(Integer.valueOf(pageNum),Integer.valueOf(pageSize),(int)Math.ceil(total.intValue() /Integer.valueOf(pageSize)), total.intValue(), communityList);
        JsonResult jsonResult = new JsonResult<>();
        jsonResult.setData(pageResult);
        jsonResult.setStatus(200);

        //处理数据json
        String jsonString = JSON.toJSONString(jsonResult);
        return jsonString;
    }
}
